package com.zdtech.action;

import java.io.File;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.zdtech.common.DataResult;
import com.zdtech.entity.TMenu;
import com.zdtech.service.BaseSerive;
import com.zdtech.util.DateUtil;

public class EditMenuAction extends ActionSupport{
	
	private TMenu menu;
	private File photo;
	private String photoFileName;
	private String photoContentType;
	private DataResult<TMenu> dataResult = new DataResult<>();
	@Resource
	private BaseSerive baseService;
	
	public String execute() throws Exception {
		if(null!=menu){
			if(null!=photo){
				String oldpath = ServletActionContext.getServletContext().getRealPath("/images/"+menu.getPhoto());
				String newpath = ServletActionContext.getServletContext().getRealPath("/images");
				StringBuffer sb = new StringBuffer();
				sb.append(DateUtil.getCurrentDate("yyyyMMddHHmmss"));
				sb.append(photoFileName);
				File savefile = new File(new File(newpath), sb.toString());
				if(new File(oldpath).exists()){
					new File(oldpath).delete();
					FileUtils.copyFile(photo, savefile);
					menu.setPhoto(sb.toString());
					baseService.editMenu(menu);
					dataResult.setSuccess(true);
					dataResult.setMessage("保存成功");
					System.out.println("保存成功");
				}else{
					dataResult.setSuccess(false);
					dataResult.setMessage("文件不存在");
					System.out.println("文件不存在");
				}
			}else{
				dataResult.setSuccess(false);
				dataResult.setMessage("photo error");
				System.out.println("photo error");
			}
		}else{
			dataResult.setSuccess(false);
			dataResult.setMessage("menu error");
			System.out.println("menu error");
		}
		return Action.SUCCESS;
	}
	public TMenu getMenu() {
		return menu;
	}
	public void setMenu(TMenu menu) {
		this.menu = menu;
	}
	public File getPhoto() {
		return photo;
	}
	public void setPhoto(File photo) {
		this.photo = photo;
	}
	public String getPhotoFileName() {
		return photoFileName;
	}
	public void setPhotoFileName(String photoFileName) {
		this.photoFileName = photoFileName;
	}
	public String getPhotoContentType() {
		return photoContentType;
	}
	public void setPhotoContentType(String photoContentType) {
		this.photoContentType = photoContentType;
	}

}
